package com.hrbxlh.actionImpl;

import com.hrbxlh.constants.VirtualMachineStatusEnum;
import com.hrbxlh.dao.VirtualMachineManageDao;
import com.hrbxlh.exception.OpenstackException;
import com.hrbxlh.exception.VirtualMachineAllocateException;
import com.hrbxlh.util.OpenstackUtil;
import com.hrbxlh.util.pageUtil;
import com.hrbxlh.websocket.VirtualMachineShareWebSocket;
import org.openstack4j.model.compute.Address;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.model.compute.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * -------------------------------------------------------
 * Copyright (c) 2017, 鑫联华信息科技股份有限公司
 * All rights reserved.
 * FileName：VirtualMachineManageActionImpl.java
 * Description：虚拟机管理actionImpl类
 * History：
 * Date           Author               Desc
 * 2017.5.5		  王晨                 建立
 * -------------------------------------------------------
 */
@Transactional
@Component("virtualMachineManageActionImpl")
public class VirtualMachineManageActionImpl {

    @Resource(name = "virtualMachineManageDao")
    private VirtualMachineManageDao virtualMachineManageDao;

    @Autowired
    @Qualifier("relationActionImpl")
    private RelationActionImpl relationActionImpl;

    /**
     * @description 获取虚拟机状态列表
     * @return
     */
    public List<Map<String, Object>> getVirtualMachineStatusList() {
        List<Map<String, Object>> virtualMachineStatusList = new ArrayList<Map<String, Object>>();
        for (VirtualMachineStatusEnum status : VirtualMachineStatusEnum.values()) {
            //屏蔽虚拟机未知的状态
            if (status.getCode() != 4) {
                Map<String, Object> virtualMachineStatusMap = new HashMap<String, Object>();
                virtualMachineStatusMap.put("statusCode", status.getCode());
                virtualMachineStatusMap.put("statusMsg", status.getMsg());
                virtualMachineStatusList.add(virtualMachineStatusMap);
            }
        }
        return virtualMachineStatusList;
    }

    /**
     * @description 更新虚拟机列表
     * @throws OpenstackException
     */
    public void updateVirtualMachineList() throws OpenstackException {
        //从openstack服务器获取虚拟机列表
        List<? extends Server> novaServers = OpenstackUtil.queryVirtualMachine();
        //查询虚拟机Id，把它们放到一个集合里
        List<Map<String, String>> vmIdList = virtualMachineManageDao.getVmIdList();
        Set<String> vmIdSet = new HashSet<String>();
        for (Map<String, String> vmIdMap : vmIdList) {
            vmIdSet.add(vmIdMap.get("vmId"));
        }
        //遍历虚拟机列表，只更新在集合里的虚拟机，更新后把虚拟机Id从集合里删除
        for (Server server : novaServers) {
            Map<String, Object> serverMap = new HashMap<String, Object>();
            String serverId = server.getId();
            serverMap.put("vmId", serverId);
            serverMap.put("vmName", server.getName());
            if (server.getFlavor() != null) {
                serverMap.put("cpu", server.getFlavor().getVcpus());
                serverMap.put("ram", server.getFlavor().getRam());
                serverMap.put("disk", server.getFlavor().getDisk());
            }
            if (server.getAddresses().getAddresses() != null && server.getAddresses().getAddresses().size() != 0) {
                for (List list : server.getAddresses().getAddresses().values()) {
                    serverMap.put("ip", ((Address) list.get(0)).getAddr());
                }
            }
            if (vmIdSet.contains(serverId)) {
                virtualMachineManageDao.updateVirtualMachine(serverMap);
                vmIdSet.remove(serverId);
            } else {
                virtualMachineManageDao.addVirtualMachine(serverMap);
            }
        }
        //检查集合剩余元素, 这些虚拟机已经不存在于openstack, 所以要清除这些无效数据
        for (String vmId : vmIdSet) {
            virtualMachineManageDao.deleteVirtualMachine(vmId);
        }
    }

    /**
     * @description 更新虚拟机配置列表
     * @throws OpenstackException
     */
    public void updateFlavor() throws OpenstackException {
        //从openstack服务器获取虚拟机模板列表
        List<? extends Flavor> flavorList = OpenstackUtil.queryFlavor();
        //查询虚拟机模板Id，把它们放到一个集合里
        List<Map<String, String>> flavorIdList = virtualMachineManageDao.getFlavorIdList();
        Set<String> flavorIdSet = new HashSet<String>();
        for (Map<String, String> flavorIdMap : flavorIdList) {
            flavorIdSet.add(flavorIdMap.get("flavorId"));
        }
        //遍历虚拟机模板列表，只更新在集合里的虚拟机模板，更新后把虚拟机模板Id从集合里删除
        Map<String, Object> flavorMap = new HashMap<String, Object>();
        for (Flavor flavor : flavorList) {
            String flavorId = flavor.getId();
            flavorMap.put("flavorId", flavorId);
            flavorMap.put("cpu", flavor.getVcpus());
            flavorMap.put("ram", flavor.getRam());
            flavorMap.put("disk", flavor.getDisk());
            if (flavorIdSet.contains(flavorId)) {
                virtualMachineManageDao.updateFlavor(flavorMap);
                flavorIdSet.remove(flavorId);
            } else {
                virtualMachineManageDao.addFlavor(flavorMap);
            }
        }
        //检查集合剩余元素, 这些虚拟机模板已经不存在于openstack, 所以要清除这些无效数据
        for (String flavorId : flavorIdSet) {
            virtualMachineManageDao.deleteFlavor(flavorId);
        }
    }

    /**
     * @description 条件及分页查询虚拟机列表
     * @param paramMap pageNow
     *                 pageSize
     *                 status
     * @return
     * @throws Exception
     */
    public Map<String, Object> getVirtualMachineList(Map<String, Object> paramMap) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int pageNow = (Integer) paramMap.get("pageNow");
        int pageSize = (Integer) paramMap.get("pageSize");
        int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
        paramMap.put("startNum", startNum);
        //获取列表信息
        List<Map<String, Object>> virtualMachineList = virtualMachineManageDao.getVirtualMachineList(paramMap);
        for (Map<String, Object> map : virtualMachineList) {
            VirtualMachineStatusEnum status = VirtualMachineStatusEnum.codeOf((Integer) map.get("status"));
            map.put("statusMsg", status.getMsg());
        }
        resultMap.put("virtualMachineList", virtualMachineList);
        resultMap.put("pageNow", pageNow);
        //获取全部页数
        paramMap.put("pageNow", 0);
        paramMap.put("startNum", 0);
        List<Map<String, Object>> allList = virtualMachineManageDao.getVirtualMachineList(paramMap);
        int totalPage = pageUtil.getTotalPage(allList, pageSize);
        resultMap.put("totalPage", totalPage);
        return resultMap;
    }

    /**
     * @description 获取虚拟机总数量、已分配虚拟机数量、未分配虚拟机数量
     * @return
     */
    public Map<String, Object> getVirtualMachineNum() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        int vmTotalNum = virtualMachineManageDao.getVirtualMachineNum(paramMap);
        paramMap.put("status", VirtualMachineStatusEnum.ALLOCATED.getCode());
        int vmAllocatedNum = virtualMachineManageDao.getVirtualMachineNum(paramMap);
        paramMap.put("status", VirtualMachineStatusEnum.UNALLOCATED.getCode());
        int vmUnAllocatedNum = virtualMachineManageDao.getVirtualMachineNum(paramMap);
        resultMap.put("vmTotalNum", vmTotalNum);
        resultMap.put("vmAllocatedNum", vmAllocatedNum);
        resultMap.put("vmUnAllocatedNum", vmUnAllocatedNum);
        return resultMap;
    }

    /**
     * @description 获取学院列表
     * @return
     */
    public List<Map<String, Object>> getDepartmentList() {
        return virtualMachineManageDao.getDepartmentList();
    }

    /**
     * @description 条件及分页查询教师列表
     * @param paramMap pageNow
     *                 pageSize
     *                 departmentId
     * @return
     * @throws Exception
     */
    public Map<String, Object> getTeacherList(Map<String, Object> paramMap) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int pageNow = (Integer) paramMap.get("pageNow");
        int pageSize = (Integer) paramMap.get("pageSize");
        int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
        paramMap.put("startNum", startNum);
        //获取列表信息
        List<Map<String, Object>> teacherList = virtualMachineManageDao.getTeacherList(paramMap);
        resultMap.put("teacherList", teacherList);
        resultMap.put("pageNow", pageNow);
        //获取全部页数
        paramMap.put("pageNow", 0);
        paramMap.put("startNum", 0);
        List<Map<String, Object>> allList = virtualMachineManageDao.getTeacherList(paramMap);
        int totalPage = pageUtil.getTotalPage(allList, pageSize);
        resultMap.put("totalPage", totalPage);
        return resultMap;
    }

    /**
     * @description 条件及分页查询班级列表
     * @param paramMap pageNow
     *                 pageSize
     *                 departmentId
     * @return
     * @throws Exception
     */
    public Map<String, Object> getClassList(Map<String, Object> paramMap) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int pageNow = (Integer) paramMap.get("pageNow");
        int pageSize = (Integer) paramMap.get("pageSize");
        int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
        paramMap.put("startNum", startNum);
        //获取列表信息
        List<Map<String, Object>> classList = virtualMachineManageDao.getClassList(paramMap);
        for (Map<String, Object> classMap : classList) {
            int stuNum = virtualMachineManageDao.getStuNumByClassId((Integer) classMap.get("classId"));
            classMap.put("stuNum", stuNum);
            int stuNumUnAllocated = virtualMachineManageDao.getStuUnallocatedList(classMap).size();
            int stuNumAllocated = stuNum - stuNumUnAllocated;
            classMap.put("stuNumAllocated", stuNumAllocated);
            classMap.put("stuNumUnAllocated", stuNumUnAllocated);
        }
        resultMap.put("classList", classList);
        resultMap.put("pageNow", pageNow);
        //获取全部页数
        paramMap.put("pageNow", 0);
        paramMap.put("startNum", 0);
        List<Map<String, Object>> allList = virtualMachineManageDao.getClassList(paramMap);
        int totalPage = pageUtil.getTotalPage(allList, pageSize);
        resultMap.put("totalPage", totalPage);
        return resultMap;
    }

    /**
     * @description 查询虚拟机配置列表
     * @return
     */
    public List<Map<String, Object>> getFlavorList(){
        return virtualMachineManageDao.getFlavorList();
    }

    /**
     * @description 删除虚拟机
     * @param paramMap vmId
     * @return
     */
    public Map<String, Object> deleteVirtualMachine(Map<String, Object> paramMap) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String vmId = (String) paramMap.get("vmId");
            if (OpenstackUtil.deleteVirtualMachine(vmId)) {
                resultMap.put("result", "success");
                virtualMachineManageDao.deleteVirtualMachine(vmId);
            } else {
                resultMap.put("result", "fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", "fail");
        }
        return resultMap;
    }

    /**
     * @description 给一个用户分配虚拟机，用户可能是教师也可能是学生
     * @param paramMap stuorteaId/classId
     *                 isTeacher
     *                 hostNum
     *                 hostCpu
     *                 hostRam
     *                 hostDisk
     *                 slaveNum
     *                 slaveCpu
     *                 slaveRam
     *                 slaveDisk
     * @throws VirtualMachineAllocateException
     */
    public void allocateVirtualMachine(Map<String, Object> paramMap) throws VirtualMachineAllocateException {
        String stuorteaId = (String) paramMap.get("stuorteaId");
        int hostNum = (Integer) paramMap.get("hostNum");
        int slaveNum = (Integer) paramMap.get("slaveNum");
        if (hostNum == 0 && slaveNum == 0) {
            throw new VirtualMachineAllocateException("主机和从机的数量不能都为0");
        }
        if (hostNum > 0) {
            int hostCpu = (Integer) paramMap.get("hostCpu");
            int hostRam = (Integer) paramMap.get("hostRam");
            int hostDisk = (Integer) paramMap.get("hostDisk");
            allocateVirtualMachine(stuorteaId, 1, hostNum, hostCpu, hostRam, hostDisk);
        }
        if (slaveNum > 0) {
            int slaveCpu = (Integer) paramMap.get("slaveCpu");
            int slaveRam = (Integer) paramMap.get("slaveRam");
            int slaveDisk = (Integer) paramMap.get("slaveDisk");
            allocateVirtualMachine(stuorteaId, 2, slaveNum, slaveCpu, slaveRam, slaveDisk);
        }
        if (paramMap.get("isTeacher") != null && (Boolean) paramMap.get("isTeacher")) {
            virtualMachineManageDao.addVirtualMachineTeacher(paramMap);
        } else if (paramMap.get("isClass") != null && (Boolean) paramMap.get("isClass") && virtualMachineManageDao.checkClassAllocated(paramMap) == 0) {
            virtualMachineManageDao.addVirtualMachineClass(paramMap);
        }
    }

    /**
     * @description 给一个用户的虚拟机集群中的分配主机或从机
     * @param stuorteaId
     * @param host
     * @param num
     * @param cpu
     * @param ram
     * @param disk
     * @throws VirtualMachineAllocateException
     */
    public void allocateVirtualMachine(String stuorteaId, int host, int num, int cpu, int ram, int disk) throws VirtualMachineAllocateException {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (virtualMachineManageDao.checkAllocated(stuorteaId) > 0) {
            throw new VirtualMachineAllocateException("该教师已分配过虚拟机");
        }
        paramMap.put("num", num);
        paramMap.put("cpu", cpu);
        paramMap.put("ram", ram);
        paramMap.put("disk", disk);
        List<Map<String, Object>> virtualMachineList = virtualMachineManageDao.getVirtualMachineUnAllocatedListByFlavor(paramMap);
        if (virtualMachineList.size() < num) {
            throw new VirtualMachineAllocateException("没有足够数量的符合配置的虚拟机");
        }
        for (Map<String, Object> virutualMachine : virtualMachineList) {
            virutualMachine.put("stuorteaId", stuorteaId);
            virutualMachine.put("host", host);
            int updateCount = virtualMachineManageDao.allocateVirtualMachine(virutualMachine);
            if (updateCount == 0) {
                throw new VirtualMachineAllocateException("虚拟机分配失败");
            }
        }
    }

    /**
     * @description 解除分配给教师的虚拟机
     * @param paramMap stuorteaId
     * @throws Exception
     */
    public void unallocateVirtualMachineTeacher(Map<String, Object> paramMap) throws Exception {
        String stuorteaId = (String) paramMap.get("stuorteaId");
        if (virtualMachineManageDao.checkAllocated(stuorteaId) == 0) {
            throw new VirtualMachineAllocateException("该用户当前没有分配虚拟机");
        }
        virtualMachineManageDao.unallocateVirtualMachine(paramMap);
        virtualMachineManageDao.deleteVirtualMachineTeacher(paramMap);
    }

    /**
     * @description 删除教师分配的虚拟机
     * @param paramMap stuorteaId
     */
    @Transactional(noRollbackFor = OpenstackException.class, propagation = Propagation.REQUIRES_NEW)
    public void deleteVirtualMachineTeacher(Map<String, Object> paramMap) throws OpenstackException {
        String stuorteaId = (String) paramMap.get("stuorteaId");
        if (virtualMachineManageDao.checkAllocated(stuorteaId) == 0) {
            throw new VirtualMachineAllocateException("该用户当前没有分配虚拟机");
        }
        List<Map<String, Object>> virtualMachineList = virtualMachineManageDao.getVirtualMachineByStuorteaId(paramMap);
        int successNum = 0;
        try {
            for (Map<String, Object> virtualMachine : virtualMachineList) {
                String vmId = (String) virtualMachine.get("vmId");
                OpenstackUtil.deleteVirtualMachine(vmId);
                virtualMachineManageDao.deleteVirtualMachine(vmId);
                successNum++;
            }
            virtualMachineManageDao.deleteVirtualMachineTeacher(paramMap);
        } catch (Exception e) {
            throw new OpenstackException("与云平台通信出现异常，在异常发生前已成功删除" + successNum + "个虚拟机");
        }
    }

    /**
     * @description 根据用户ID查询分配给其的虚拟机详细信息
     * @param paramMap stuorteaId
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> getVirtualMachineByStuorteaId(Map<String, Object> paramMap) throws Exception {
        return virtualMachineManageDao.getVirtualMachineByStuorteaId(paramMap);
    }

    /**
     * @description 获取教师详细信息
     * @param paramMap stuorteaId
     * @return
     */
    public Map<String, Object> getTeacherDetail(Map<String, Object> paramMap) {
        return virtualMachineManageDao.getTeacherDetail(paramMap);
    }

    /**
     * @description 替换虚拟机
     * @param paramMap stuorteaId
     *                 vmId
     *                 broken
     *                 msg
     *                 host
     *                 cpu
     *                 ram
     *                 disk
     * @return
     * @throws VirtualMachineAllocateException
     */
    public Map<String, Object> replaceVirtualMachine(Map<String, Object> paramMap) throws VirtualMachineAllocateException {
        if (virtualMachineManageDao.checkVirtualMachine(paramMap) == 0) {
            throw new VirtualMachineAllocateException("参数不正确");
        }
        paramMap.put("num", 1);
        List<Map<String, Object>> virtualMachineList = virtualMachineManageDao.getVirtualMachineUnAllocatedListByFlavor(paramMap);
        if (virtualMachineList.size() == 0) {
            throw new VirtualMachineAllocateException("没有足够数量的符合配置的虚拟机");
        }
        Map<String, Object> virtualMachine = virtualMachineList.get(0);
        int broken = (Integer) paramMap.get("broken");
        if(broken == 1){
            virtualMachineManageDao.markBrokenVirtualMachine(paramMap);
        } else {
            virtualMachineManageDao.unallocateVirtualMachine(paramMap);
        }
        paramMap.put("vmId", virtualMachine.get("vmId"));
        paramMap.put("id", virtualMachine.get("id"));
        int updateCount = virtualMachineManageDao.allocateVirtualMachine(paramMap);
        if (updateCount == 0) {
            throw new VirtualMachineAllocateException("虚拟机分配失败");
        }
        return virtualMachine;
    }

    /**
     * @description 查询未分配虚拟机的学生列表
     * @param paramMap classId
     * @return
     */
    public List<Map<String, Object>> getStuUnallocatedList(Map<String, Object> paramMap) {
        return virtualMachineManageDao.getStuUnallocatedList(paramMap);
    }

    /**
     * @description 解除分配给班级的虚拟机
     * @param paramMap classId
     * @throws VirtualMachineAllocateException
     */
    public void unallocateVirtualMachineClass(Map<String, Object> paramMap) throws VirtualMachineAllocateException{
        if (virtualMachineManageDao.checkClassAllocated(paramMap) == 0) {
            throw new VirtualMachineAllocateException("该班级当前没有分配虚拟机");
        }
        virtualMachineManageDao.unallocateVirtualMachineByClass(paramMap);
        virtualMachineManageDao.deleteVirtualMachineClass(paramMap);
    }

    /**
     * @description 删除分配给班级的虚拟机
     * @param paramMap classId
     * @throws VirtualMachineAllocateException
     * @throws OpenstackException
     */
    @Transactional(noRollbackFor = OpenstackException.class, propagation = Propagation.REQUIRES_NEW)
    public void deleteVirtualMachineClass(Map<String, Object> paramMap) throws VirtualMachineAllocateException, OpenstackException{
        if (virtualMachineManageDao.checkClassAllocated(paramMap) == 0) {
            throw new VirtualMachineAllocateException("该班级当前没有分配虚拟机");
        }
        int successNum = 0;
        try {
            List<Map<String, Object>> stuList = virtualMachineManageDao.getStuListByClass(paramMap);
            for (Map<String, Object> stu : stuList) {
                paramMap.put("stuorteaId", stu.get("stuorteaId"));
                List<Map<String, Object>> virtualMachineList = virtualMachineManageDao.getVirtualMachineByStuorteaId(paramMap);
                for (Map<String, Object> virtualMachine : virtualMachineList) {
                    String vmId = (String) virtualMachine.get("vmId");
                    OpenstackUtil.deleteVirtualMachine(vmId);
                    virtualMachineManageDao.deleteVirtualMachine(vmId);
                    successNum++;
                }
            }
            virtualMachineManageDao.deleteVirtualMachineClass(paramMap);
        } catch (Exception e) {
            throw new OpenstackException("与云平台通信出现异常，在异常发生前已成功删除" + successNum + "个虚拟机");
        }
    }

    /**
     * @description 查询班级基本信息
     * @param paramMap classId
     * @return
     */
    public Map<String, Object> getClassDetail(Map<String, Object> paramMap) {
        return virtualMachineManageDao.getClassDetail(paramMap);
    }

    /**
     * @description 查询班级每个学生虚拟机分配情况
     * @param paramMap classId
     * @return
     */
    public Map<String, Object> getVirtualMachineListByClass(Map<String, Object> paramMap) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int pageNow = (Integer) paramMap.get("pageNow");
        int pageSize = (Integer) paramMap.get("pageSize");
        int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
        paramMap.put("startNum", startNum);
        //获取列表信息
        List<Map<String, Object>> stuList = virtualMachineManageDao.getStuListByClass(paramMap);
        for (Map<String, Object> stuMap : stuList) {
            List<Map<String, Object>> virtualMachineList = getVirtualMachineByStuorteaId(stuMap);
            stuMap.put("virtualMachineList", virtualMachineList);
        }
        resultMap.put("stuList", stuList);
        resultMap.put("pageNow", pageNow);
        //获取全部页数
        paramMap.put("pageNow", 0);
        paramMap.put("startNum", 0);
        List<Map<String, Object>> allList = virtualMachineManageDao.getStuListByClass(paramMap);
        int totalPage = pageUtil.getTotalPage(allList, pageSize);
        resultMap.put("totalPage", totalPage);
        return resultMap;
    }

    /**
     * @description 教师查询实验共享成员列表
     * @param paramMap courseId
     *                 stuorteaId
     * @return
     */
    public List<Map<String,Object>> getTeacherShareMemberList(Map<String, Object> paramMap) throws Exception{
        List<Map<String, Object>> stuList = virtualMachineManageDao.getStuListByCourseTeacher(paramMap);
        List<Map<String, Object>> shareMemberList = new ArrayList<Map<String, Object>>();
        Set<String> memberSet = VirtualMachineShareWebSocket.getMemberSet();
        for (Map<String, Object> stuMap : stuList) {
            if(memberSet.contains(stuMap.get("stuorteaId"))){
                shareMemberList.add(stuMap);
            }
        }
        return shareMemberList;
    }

    /**
     * @description 学生查询实验共享成员列表
     * @param paramMap courseId
     *                 stuorteaId
     * @return
     */
    public Map<String,Object> getStudentShareMember(Map<String, Object> paramMap) throws Exception {
        paramMap.put("roleId", 1);
        String stuId = (String) paramMap.get("stuorteaId");
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Set<String> memberSet = VirtualMachineShareWebSocket.getMemberSet();
        Map<String, Object> teacher = relationActionImpl.getMyTeacherByCourseId(paramMap);
        if(memberSet.contains(teacher.get("stuorteaId"))){
            resultMap.put("stuorteaId", teacher.get("stuorteaId"));
            resultMap.put("userName", virtualMachineManageDao.getUserName((String) teacher.get("stuorteaId")));
        }
        paramMap.put("stuorteaId", teacher.get("stuorteaId"));
        List<Map<String, Object>> stuList = virtualMachineManageDao.getStuListByCourseTeacher(paramMap);
        List<Map<String, Object>> shareMemberList = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> stuMap : stuList) {
            if(memberSet.contains(stuMap.get("stuorteaId"))){
                if(!stuMap.get("stuorteaId").equals(stuId)){
                    shareMemberList.add(stuMap);
                }
            }
        }
        resultMap.put("shareMemberList", shareMemberList);
        return resultMap;
    }

    /**
     * @description 查询某教师某课程下的学生列表
     * @param stuorteaId
     * @return
     */
    public String getUserName(String stuorteaId) {
        return virtualMachineManageDao.getUserName(stuorteaId);
    }
    
    /**
     * @description 删除学生分配的虚拟机
     * @param paramMap stuorteaId
     */
    @Transactional(noRollbackFor = OpenstackException.class)
    public void deleteVirtualMachineStudent(Map<String, Object> paramMap) throws OpenstackException {
        String stuorteaId = (String) paramMap.get("stuorteaId");
        List<Map<String, Object>> virtualMachineList = virtualMachineManageDao.getVirtualMachineByStuorteaId(paramMap);
        int successNum = 0;
        try {
            for (Map<String, Object> virtualMachine : virtualMachineList) {
                String vmId = (String) virtualMachine.get("vmId");
                OpenstackUtil.deleteVirtualMachine(vmId);
                virtualMachineManageDao.deleteVirtualMachine(vmId);
                successNum++;
            }
        } catch (Exception e) {
            throw new OpenstackException("与云平台通信出现异常，在异常发生前已成功删除" + successNum + "个虚拟机");
        }
    }

}
